using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Data;
using System.IO;
namespace Test
{
  class Auto
  {
    static void MainX()
    {
      SQL sql=new SQL("(local)\\SQLEXPRESS","ph");
      Cache cache=new Cache("atr");
      string[] urls=File.ReadAllLines("AutoData.txt");
      //System.Diagnostics.Stopwatch sw=System.Diagnostics.Stopwatch.StartNew();
      //DataTable links=sql.Query("select link from carSrc");
      //sql.NonQry("truncate table car");
      foreach(string url in urls)
      {
        int current=0;
        int pages=0;
        do
        {
          string html=cache.Get(url+"/page/"+current,10);
          //Console.WriteLine(sw.Elapsed);
          Nod nod=new Nod(html);
          if(pages==0)
          {
            try
            {
              string btnLast=nod.Doubles("<a class=\"button last\"")[0];
              string ilast=new Regex("page/(.+?)\"").Match(btnLast).Groups[1].Value;
              pages=int.Parse(ilast);
            }
            catch { }
          }
          string[] ads=nod.Doubles("<div id=\"advert[0-9]{1,2}CarDetails\">");
          foreach(string ad in ads)
          {
            Nod adNod=new Nod(ad);
            string srcId=adNod.Regex("<var class=\"advertId\" title=\"(.+?)\"></var>")[0];
            int srcIdCnt=(int)sql.Scalar("select count(*)from car(nolock)where srcId='{0}'",srcId);
            if(srcIdCnt==0)
            {
              try
              {
                string distance=adNod.Regex("<span id=\"advert[0-9]{1,2}distance\" class=\"distanceAmount\">(.+?) miles</span>")[0];
                string price=adNod.Regex("<span data-test=\"price-label-deal-price\">&pound;(.+?)</span>")[0].Replace(",","");
                string[] imgMain=adNod.Regex("<img.+?\\?id=(.+?)&amp.+?>");
                string[] imgRest=adNod.Regex("url\\(http://pictures2.autotrader.co.uk/imgser-uk/servlet/media\\?id=(.+?)&amp;width=57&amp;height=43\\)");
                List<string> imgs=new List<string>();
                foreach(string img in imgMain)
                  if(!imgs.Contains(img))
                    imgs.Add(img);
                foreach(string img in imgRest)
                  if(!imgs.Contains(img))
                    imgs.Add(img);
                string imgsCsv=string.Join(",",imgs.ToArray());
                string miles=adNod.Regex("<span class=\"mileage\">(.+?) miles</span>")[0].Replace(",","");
                char trans=adNod.Regex("<span class=\"transmission\">(.+?)</span>")[0][0];
                string[] engSize=adNod.Regex("<span class=\"engine\">(.+?)L</span>");
                string engine=engSize.Length==1?engSize[0]:null;
                char fuel=adNod.Regex("<span class=\"fuel\">(.+?)</span>")[0][0];
                //string[] items=adNod.Doubles("<li");
                string yearNtype=adNod.Regex("<h3 id=\"advert[0-9]{1,2}advertTitleSub\">(.+?)</h3>")[0];
                string year=yearNtype.Split(' ')[0];
                char bType=yearNtype.Split(',')[1].Trim()[0];
                char seller=adNod.Regex("<span class=\"advertType\">(.+?)</span>")[0][0];
                //string desc=adNod.Regex("<div id=\"advert[0-9]{1,2}advertDescription\" class=\"searchResultMainText\">(.+?)</div>")[0].Trim();
                string title=adNod.Regex("<a class=\"external title\".+?rel=\"nofollow\">(.+?)</a>")[0].Trim();
                sql.NonQry("insert into car(srcId,title,miles,price,fuel,trans,engine,seller,year,bodytype,imgs,distance)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')",srcId,title,miles,price,fuel,trans,engine,seller,year,bType,imgsCsv,distance);
                Console.WriteLine(srcId);
              }
              catch(Exception ex)
              {
                Console.WriteLine("Exception:"+srcId+", "+ex.Message);
              }
            }
          }
        } while(current++<pages);
      }
    }
  }
}
